﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;
using System.Data;
using System.Configuration;
//using MySql.Data;
using System.Data.Odbc;
//using MySql.Data.MySqlClient;
using System.Data.SqlClient;

namespace KPIS.GERP.GHRMS.DAL
{
    public class TrainingCoursesTrainerDAL : ITrainingCoursesTrainerDAL
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();
        private SqlConnection conn = new SqlConnection(SystemInfo._ConnectionString);

        public IList<TrainingCoursesTrainerInfo> GetFull(Nullable<int> train_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<TrainingCoursesTrainerInfo> infoList = new List<TrainingCoursesTrainerInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@train_seq", train_seq);

                strSQL = "SELECT ct.course_trainer_seq"
                        + ", ct.trainer_seq"
                        + ", ct.training_subject_seq"
                        + ", ct.training_subject_dtls_seq"
                        + ", ct.remarks"
                        + ", ht.first_name_local"
                        + ", ht.last_name_local"
                        + ", pf.prefix_short"
                        + " FROM hrm_training_courses_trainers ct"
                        + " Left Join hrm_trainers ht ON ct.trainer_seq = ht.trainer_seq"
                        + " Left Join mas_name_prefixes pf ON ht.name_prefix_seq = pf.name_prefix_seq"
                        + " WHERE ct.record_status not like @delete_status"
                        + (train_seq > 0 ? " AND ct.train_seq = @train_seq " : "");


                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        TrainingCoursesTrainerInfo info = new TrainingCoursesTrainerInfo();
                        info.Sequence = Convert.ToInt32(drc[i]["course_trainer_seq"]);
                        info.Trainer = drc[i]["prefix_short"].ToString() + " " + drc[i]["first_name_local"].ToString() + " " + drc[i]["last_name_local"].ToString();
                        info.Remarks = drc[i]["remarks"].ToString();
                        //info.job_site_ref = drc[i]["job_site_ref"].ToString();
                        //info.train_seq = Convert.ToInt32(drc[i]["train_seq"]);
                        //info.training_course_descr = drc[i]["training_course_descr"].ToString();
                        //info.quota = Convert.ToInt32(drc[i]["quota"]);
                        //info.Area_name = drc[i]["work_area_name"].ToString();

                        infoList.Add(info);

                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public IList<TrainingCoursesTrainerInfo> GetList(Nullable<int> Sequence)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<TrainingCoursesTrainerInfo> infoList = new List<TrainingCoursesTrainerInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", Sequence);

                strSQL = "SELECT ct.course_trainer_seq"
                        + ", ct.trainer_seq"
                        + ", ct.training_subject_seq"
                        + ", ct.training_subject_dtls_seq"
                        + ", ct.remarks"
                        + ", ht.first_name_local"
                        + ", ht.last_name_local"
                        + ", pf.prefix_short"
                        + " FROM hrm_training_courses_trainers ct"
                        + " Left Join hrm_trainers ht ON ct.trainer_seq = ht.trainer_seq"
                        + " Left Join mas_name_prefixes pf ON ht.name_prefix_seq = pf.name_prefix_seq"
                        + " WHERE ct.record_status not like @delete_status"
                        + (Sequence > 0 ? " AND course_trainer_seq = @Sequence " : "");

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {

                        TrainingCoursesTrainerInfo info = new TrainingCoursesTrainerInfo();
                        info.Sequence = Convert.ToInt32(drc[i]["course_trainer_seq"]);
                        info.Trainer = drc[i]["prefix_short"].ToString() + drc[i]["first_name_local"].ToString() + " " + drc[i]["last_name_local"].ToString();
                        info.Remarks = drc[i]["remarks"].ToString();


                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public void Insert(TrainingCoursesTrainerInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);

                //dbManager.AddParameters(0, "@TrainingSubjectSeq", info.TrainingSubjectSeq);
                //dbManager.AddParameters(1, "@TrainingSubjectDtlsSeq", info.TrainingSubjectDtlsSeq);
                dbManager.AddParameters(0, "@TrainerSeq", info.TrainerSeq);
                dbManager.AddParameters(1, "@Remarks", info.Remarks);
                dbManager.AddParameters(2, "@TrainSeq", info.TrainSeq);
                dbManager.AddParameters(3, "@record_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(4, "@created_by", info.CreatedBy);
                dbManager.AddParameters(5, "@created_when", DateTime.Today);


                strSQL = "INSERT INTO hrm_training_courses_trainers"
                    + "(trainer_seq"
                    + ", train_seq"
                    + ", remarks"
                    + ", record_status"
                    + ", created_by"
                    + ", created_when)"
                    + " VALUES (@TrainerSeq"
                    + ", @TrainSeq"
                    + ", @Remarks"
                    + ", @record_status"
                    + ", @created_by"
                    + ", @created_when)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(TrainingCoursesTrainerInfo info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Sequence", info.Sequence);
                dbManager.AddParameters(1, "@TrainingSubjectSeq", info.TrainingSubjectSeq);
                dbManager.AddParameters(2, "@TrainingSubjectDtlsSeq", info.TrainingSubjectDtlsSeq);
                dbManager.AddParameters(3, "@Remarks", info.Remarks);
                dbManager.AddParameters(4, "@updated_by", info.UpdatedBy);
                dbManager.AddParameters(5, "@updated_when", DateTime.Today);

                strSQL = "UPDATE hrm_training_courses_trainers"
                    + " SET training_subject_seq = @TrainingSubjectSeq"
                    + ", training_subject_dtls_seq = @TrainingSubjectDtlsSeq"
                    + ", remarks = @Remarks"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE course_trainer_seq = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int Sequence, int updated_by)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", updated_by);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE hrm_training_courses_trainers"
                    + " SET record_status = @record_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE course_trainer_seq = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public DataTable LoadPerson()
        {
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = "hr_sp_traing_course_person_add";
                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dtb = new DataTable();
                da.Fill(dtb);

                return dtb;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
